<template>
  <div>
    <el-form :inline="true" :model="page" style="margin-top: 20px;">
      <el-form-item label="账号">
        <el-input v-model="page.params.username" clearable placeholder="账号" />
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="page.params.name" clearable placeholder="姓名" />
      </el-form-item>
      <el-form-item label="性别">
        <template>
          <el-select v-model="page.params.sex" placeholder="请选择性别" clearable filterable @clear="clearSelect">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="page.params.nickname" clearable placeholder="昵称" />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="page.params.userEmail" clearable placeholder="邮箱" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <el-button type="primary" size="mini" class="addBtn" @click="openUserAddDialog()">添加</el-button>
    <el-button type="danger" size="mini" class="addBtn" @click="deleteByIds">批量删除</el-button>
    <el-button type="danger" size="mini" class="addBtn" @click="resetByIds">批量重置密码</el-button>
    <el-table
      v-loading="loading"
      :data="page.list"
      border
      style="width: 100%"
      element-loading-text="数据加载中"
      element-loading-spinner="el-icon-loading"
      @selection-change="handlerSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column prop="userId" label="ID" width="50" />
      <el-table-column prop="username" label="账号" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="sex" label="性别" sortable="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex===1" type="primary">男</el-tag>
          <el-tag v-else type="danger">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="header"
        label="头像"
        align="center"
      >
        <template slot-scope="scope">

          <el-image
            style="width: 100px;min-height: 60px"
            :src="scope.row.header"
            :preview-src-list="[scope.row.header]"
          >
            <div
              slot="placeholder"
              v-loading="true"
              element-loading-text="图片加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="#fff"
              class="image-slot"
              style="height: 60px;width: 100%"
            />
            <div slot="error" class="image-slot" style="width:100px;height:60px;line-height:60px;background-color: #e2e2e2">
              load failed
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="用户昵称"
        align="center"
      />
      <el-table-column
        prop="userEmail"
        label="用户邮箱"
        align="center"
      />
      <el-table-column
        prop="createdTime"
        label="创建时间"
        align="center"
        sortable="custom"
      />
      <el-table-column
        prop="updateTime"
        label="更新时间"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-dropdown trigger="hover">
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handlerEdit(scope.row.userId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handlerDelete(scope.row.userId)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handlerResetPwd(scope.row.userId)">重置密码</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>

      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.countTotal"
      class="pagination-bottom"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!--添加用户-->
    <el-dialog
      v-if="userAddDialog"
      title="添加用户"
      :visible.sync="userAddDialog"
      width="50%"
    >
      <user-add @getUserList="getUserList" @closeAddUserDialog="closeAddUserDialog" />
    </el-dialog>
    <!--更新用户-->
    <el-drawer
      v-if="userUpdateDialog"
      title="更新用户"
      :visible.sync="userUpdateDialog"
      width="30%"
    >
      <user-update :user="user" @getUserList="getUserList" @closeUserUpdateDialog="closeUserUpdateDialog" />
    </el-drawer>
  </div>
</template>

<script>
import userApi from '../../../api/myuser'
import userAdd from './user-add'
import userUpdate from './user-update'
export default {
  name: 'UserList',
  components: {
    userAdd,
    userUpdate
  },
  data() {
    return {
      page: {
        currentPage: 0, // 当前页
        pageSize: 10, // 页面大小
        pageTotal: 0, // 总页数
        countTotal: 0, // 总条数
        sortColumn: '', // 排序字段
        sortMethod: 'asc', // 排序方法
        list: [], // 数据
        params: {} // 条件查询参数
      },
      user: {
      },
      userAddDialog: false,
      userUpdateDialog: false, // 修改用户内容
      userInfoDialog: false,
      multipleSelection: [],
      userInfoContent: {}, // 用户内容
      loading: true
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getUserList()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getUserList()
    },
    // 自定义排序
    sortChange(e) {
      this.page.sortColumn = e.prop
      this.page.sortMethod = e.order
      this.getUserList()
    },
    // 用户修改点击
    handlerEdit(userId) {
      this.userUpdateDialog = true
      userApi.get(userId).then(res => {
        if (res.code === 200) {
          this.user = res.data
          this.$message.success('已获取当前用户信息')
        } else {
          this.$message.error('获取当前用户信息失败')
        }
      })
    },
    clearSelect() {
      delete this.page.params.sex
    },
    // 多选框事件
    handlerSelectionChange(val) {
      this.multipleSelection = []
      val.forEach(res => {
        this.multipleSelection.push(res.userId)
      })
    },
    // 多选删除
    deleteByIds(ids) {
      this.$confirm('此操作为批量删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userApi.deleteByIds(this.multipleSelection).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getUserList()
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    },
    getUserList() {
      userApi.getByPage(this.page).then(res => {
        this.page = res.data
        this.loading = false
      })
    },
    // 查询
    onSubmit() {
      const loading = this.$loading({
        lock: true,
        text: '查询中请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      userApi.getByPage(this.page).then(res => {
        if (res.code === 200) {
          this.page = res.data
        } else {
          this.$message.error(res.msg)
        }
        loading.close()
      }, err => {
        this.$message.error(err)
        loading.close()
      })
    },
    handlerDelete(id) {
      this.$confirm('此操作将删除该篇用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        userApi.deleteById(id).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getUserList()
          } else {
            this.loading = false
          }
        }).catch(e => {
          this.loading = false
        })
      })
    },
    handlerResetPwd(id) {
      this.$confirm('此操作将重置该用户密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userApi.resetPwd(id).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
          }
        })
      })
    },
    resetByIds() {
      this.$confirm('此操作将批量重置用户密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.multipleSelection.length === 0) {
          this.$message.error('未选择')
          return
        }
        userApi.resetPwdByIds(this.multipleSelection).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
          }
        })
      })
    },
    // 打开添加用户弹窗
    openUserAddDialog() {
      this.userAddDialog = true
    },
    // 关闭添加用户弹窗
    closeAddUserDialog() {
      this.userAddDialog = false
    },
    // 打开修改用户弹窗
    openUserUpdateDialog() {
      this.userUpdateDialog = true
    },
    // 关闭用户修改弹窗
    closeUserUpdateDialog() {
      this.userUpdateDialog = false
    }
  }
}
</script>

<style scoped>
  .el-form-item{
    margin-bottom: 0;
  }
</style>

